package com.dgut.legalservice.dao;

import com.dgut.legalservice.domain.attachment.*;
import com.dgut.legalservice.domain.contract.Contract;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IAttachmentDao {

    /**
     * 新增附件
     * @param attachment
     * @return
     */
    @Insert("insert into t_sys_attachment(name,path,description,createTime,updateTime,createBy,updateBy) " +
            "values (#{name},#{path},#{description},now(),now(),#{createBy},#{updateBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int insertAttachment(Attachment attachment);

    /**
     * 新增附件组-附件关联
     *
     * @param attId
     * @param groupId
     * @return
     */
    @Insert("insert into t_sys_att_group_ref(groupId,attId) values (#{groupId},#{attId})")
    public int insertAttGroupRef(Integer groupId, Integer attId);

    /**
     * 新增附件组
     * @param attachmentGroup
     * @return
     */
    @Insert("insert into t_sys_attachment(name,description,createTime,updateTime,createBy,updateBy) " +
            "values (#{name},#{description},now(),now(),#{createBy},#{updateBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int insertAttachmentGroup(AttachmentGroup attachmentGroup);

    /**
     * id查找单个附件
     * @param id
     * @return
     */
    @Select("select * from t_sys_attachment where id=#{id}")
    public Attachment findAttachmentById(Integer id);

    /**
     * groupId查找附件组的所有附件
     * @param groupId
     * @return
     */
    @Select("select it.* from t_sys_att_group_ref agr " +
            "left join t_sys_attachment at on at.id=agr.attId " +
            "where agr.groupId=#{groupId}")
    public List<Attachment> findAttachmentsByGroupId(Integer groupId);

    /**
     * 更新附件
     * @param attachment
     * @return
     */
    @Update("update t_sys_attachment set name=#{name},path=#{path}," +
            "description=#{description},updateTime=now(),updateBy=#{updateBy} where id=#{id}")
    public int updateAttachment(Attachment attachment);

    /**
     * 删除附件
     * @param id
     */
    @Delete("delete from t_sys_attachment where id=#{id}")
    public void deleteAttachment(Integer id);


}
